package com.orangeforms.webadmin.app.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.orangeforms.common.core.annotation.MyDataSource;
import com.orangeforms.common.core.base.dao.BaseDaoMapper;
import com.orangeforms.common.core.base.service.BaseService;
import com.orangeforms.common.core.object.CallResult;
import com.orangeforms.common.core.object.MyRelationParam;
import com.orangeforms.webadmin.app.dao.SysProjectMapper;
import com.orangeforms.webadmin.app.model.SysProject;
import com.orangeforms.webadmin.app.service.SysContractInfoService;
import com.orangeforms.webadmin.app.service.SysProjectService;
import com.orangeforms.webadmin.config.DataSourceType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * OrangeSingleDemo SysProjectServiceImpl class
 *
 * @author xisheng
 * @date 2022/08/10
 */
@Slf4j
@Service("sysProjectService")
@MyDataSource(DataSourceType.MAIN)
public class SysProjectServiceImpl extends BaseService<SysProject, Long> implements SysProjectService {

    @Autowired
    private SysProjectMapper sysProjectMapper;

    @Override
    protected BaseDaoMapper<SysProject> mapper() {
        return sysProjectMapper;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public SysProject saveNew(SysProject sysProject) {
        sysProjectMapper.insert(this.buildDefaultValue(sysProject));
        return sysProject;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveNewBatch(List<SysProject> sysProjectList) {
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean update(SysProject sysProject, SysProject originalSysProject) {

        UpdateWrapper<SysProject> uw = this.createUpdateQueryForNullValue(sysProject,sysProject.getId());
        return sysProjectMapper.update(sysProject,uw) == 1;
    }

    @Override
    public boolean remove(Long id) {
        return sysProjectMapper.deleteById(id) == 1;
    }

    @Override
    public List<SysProject> getSysProjectList(SysProject filter, String orderBy) {
        return sysProjectMapper.getSysProjectList(filter,orderBy);
    }

    @Override
    public List<SysProject> getSysProjectListWithRelation(SysProject filter, String orderBy) {
        List<SysProject> resultList = sysProjectMapper.getSysProjectList(filter,orderBy);
        int batchSize = resultList instanceof Page ? 0 : 1000;
        this.buildRelationForDataList(resultList, MyRelationParam.normal(),batchSize);
        return resultList;
    }

    @Override
    public CallResult verifyRelatedDataBeforeDelete(SysProject sysProject) {
        return null;
    }

    @Override
    public List<SysProject> getGroupedSysProjectListWithRelation(
            SysProject filter, String groupSelect, String groupBy, String orderBy) {
        List<SysProject> resultList = sysProjectMapper.getGroupedSysProjectList(filter,groupSelect,groupBy,orderBy);
        int batchSize = resultList instanceof com.github.pagehelper.Page ? 0 : 1000;
        this.buildRelationForDataList(resultList, MyRelationParam.normal(), batchSize);
        return resultList;
    }

    private SysProject buildDefaultValue(SysProject sysProject) {
        sysProject.setId(IdUtil.getSnowflakeNextId());
        return sysProject;
    }
}
